30. Remaining Words

Remaining Words

Question:

Adding the Remaining Words

You’ve successfully added a ListView and a custom ArrayAdapter to your app to display the English and Miwok words for the Numbers category.

Step 1:

Finish modifying the ColorsActivity, FamilyActivity, and PhrasesActivity so we see the English and Miwok translations for all words in the app. See the Miwok Dictionary document for the list of the other words.

We don’t have images yet, but that will come in the next step.


Hint #1: To get started with the ColorsActivity, instead of writing all the list code again from scratch, start off by copying the NumbersActivity code over to the ColorsActivity file. We’ll use this as a base and then modify as needed. Rename the class to be called ColorsActivity (so it matches the name of the file).


Step: 2

Run it on the device to make sure the ColorsActivity is displaying something - even though it’s still the wrong vocab words. We can switch them later.

Step: 3

Ok, we know the code works, we just need to swap out the numbers with the color words. You can repeat the copy/pasting for the other activities as well. Run your app again to make sure everything worked.


Hint #2: To save yourself some extra work, we can reuse the activity_numbers.xml for all 4 activities, instead of updating activity_colors.xml, activity_family.xml, and activity_phrases.xml to also have a ListView XML element. The activity_numbers.xml has nothing specific to numbers in it because all the words are added to the list dynamically when the app is running via Java code.

activity_numbers.xml file

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.android.miwok.NumbersActivity"/>

Remember the tools attribute is Numbers-specific, so we can remove that.


Let’s just pick a better name for the activity_numbers.xml file to make it more general to all the categories (not just the Numbers category). How about renaming the file to word_list.xml?

Select activity_numbers.xml -> Refactor -> Rename:
Remaining_Words_Rename

Rename to word_list.xml:

Remaining_Words_Rename_Dialog

Then click the “Do Refactor” button. All occurrences of activity_numbers will be replaced with the new name: word_list:

Remaining_Words_Refactor

Step: 4

Now we can set the content view of each activity to be word_list layout.

Example:

public class ColorsActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.word_list);

Be sure to delete any unused layout files such as activity_colors.xml, activity_phrases.xml, and activity_family.xml.

Start Quiz:

Solution:

INSTRUCTOR NOTE:

Note

See the Miwok Dictionary for the lists of words for colors, family members, and phrases.


These are the changes needed to complete the quiz.

This is the state of the code after completing the quiz.